<!DOCTYPE html>
<html lang="en">
 <head>
	<meta charset="UTF-8">
	<meta name="Generator" content="EditPlus®">
	<meta name="Author" content="">
	<meta name="Keywords" content="">
	<meta name="Description" content="">
	<title>设置cookie,每个客户端只能点赞一次</title>
	<style>
		a{
			cursor: pointer;
		}
	</style>
 </head>
 <body>
	<div class="setLike">
		<a onclick="comment(id)"><img src="like.png" width="30" height="30"></a>
	</div>
	<script type="text/javascript">
        //判断是否点赞过，若点赞过则显示已点赞图标
		// if(getCookie(title+author)==null){
		// 	$('#like_img').css("background-image","url(../images/unlike.png)");
		// }else if(getCookie(title+author)=="setlike"+title+author){
		// 	$('#like_img').css("background-image","url(../images/like.png)");
		// }

		//设置cookie
		function setCookie(key, value,expiredays) {
			var exdate=new Date()
			exdate.setDate(exdate.getDate()+expiredays)
			document.cookie=key+ "=" +escape(value)+
			((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
		}
		//获取cookie的值
		function getCookie(key) {
			if (document.cookie.length) {
				var cookies = ' ' + document.cookie;
				var start = cookies.indexOf(' ' + key + '=');
				if (start == -1) { return null; }
				var end = cookies.indexOf(";", start);
				if (end == -1) { end = cookies.length; }
				end -= start;
				var cookie = cookies.substr(start,end);
				return unescape(cookie.substr(cookie.indexOf('=') + 1, cookie.length - cookie.indexOf('=') + 1));
			}
			else { return null; }
		}    
		//根据点击传进来的id
		function comment(id){
			id="like";
			if(getCookie(id)==null){
				setCookie(id,"setlike",730);      //730:cookie过期天数
				alert("设置cookie成功");
			}else{
				if(getCookie(id)=="setlike"){   
					alert("您已经点赞过了");
					return;
				}
			}
		 //这里是你自己的逻辑 通过ajax保存到数据库的数值
		}	
	</script>
 </body>
</html>